package com.demo.pojo;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author leiliang
 * @version 1.0
 * @Description
 * @date 2021/6/22 22:23
 */
public class Testdigui {


    static List<TreeEntity>  listTree = new ArrayList<>();
    static {
        listTree.add(new TreeEntity(1,"爷",0,null));
        listTree.add(new TreeEntity(2,"二爷",0,null));
        listTree.add(new TreeEntity(3,"爷-爸",1,null));
        listTree.add(new TreeEntity(4,"爷-叔",1,null));
        listTree.add(new TreeEntity(5,"二爷-爸",2,null));
        listTree.add(new TreeEntity(6,"爷-爸-大儿子",3,null));
        listTree.add(new TreeEntity(7,"爷-爸-小儿子",3,null));
        listTree.add(new TreeEntity(8,"二爷-爸-大儿子",5,null));
        listTree.add(new TreeEntity(9,"二爷-爸-小儿子",5,null));
        listTree.add(new TreeEntity(10,"爷-叔-大儿子",4,null));
        listTree.add(new TreeEntity(11,"爷-叔-小儿子",4,null));
    }
    /**
     * 获取树结构
     * @return
     *
     */


    public static void main(String[] args) {
        List<TreeEntity> treeEntities = selectTree();
        System.out.println(JSONObject.toJSONString(treeEntities));
    }

    public static List<TreeEntity> selectTree() {
        List<TreeEntity> leve1Menus = listTree.stream().filter(treeEntity ->
                treeEntity.getParentId() == 0
        ).map((menus) ->{
            menus.setChildNodeList(getChildNode(menus,listTree));
            return menus;
        }).collect(Collectors.toList());
        return leve1Menus;
    }




    /**
     * 递归子节点
     * @param root   当前单个菜单
     * @param allListTree   表中的所有菜单集合
     * @return
     */
    private static List<TreeEntity> getChildNode(TreeEntity root, List<TreeEntity> allListTree){

        List<TreeEntity> ChildNodeList = allListTree.stream().filter((treeEntity) -> {
            return treeEntity.getParentId() == root.getId();
        }).map((treeEntity)->{
            treeEntity.setChildNodeList(getChildNode(treeEntity,allListTree));
            return treeEntity;
        }).collect(Collectors.toList());
        return ChildNodeList;
    }

}
